//***************************************************************************************
// MediaFrame is an Open Source streaming media platform in Java 
// which provides a fast, easy to implement and extremely small applet 
// that enables to view your audio/video content without having 
// to rely on external player applications or bulky plug-ins.
//
//--------------------------------------------------------------------------------------
//
// We changed a lot of code and added a lot of functionality.
// This includes, but not limited to, the following changes:
// 1. The project was renamed to MediaFrame;
// 2. The connection speed detection procedure was added;
// 3. The JavaScript API functions were added;
// 4. The pre and post image support functionality was added;
// 5. The ability to save movie into the local disk was added;
// 6. The inner buffer for a movie file was added;
// 7. The click-through functionality was added;    
// 8. The .zip files support was added;    
// 9. The realtime feedback agent functionality was added.    
// For the full list of the current functionality please visit the following web page:
// http://mediaframe.org/
//    
// 06 Jul 2002 - 19 Dec 2004 Konstantin Belous, Oleg Lebedev
//
//--------------------------------------------------------------------------------------
//
// This program is free software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with this
// program; (See "LICENSE.GPL"). If not, write to the Free Software Foundation, Inc.,
// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
//--------------------------------------------------------------------------------------
//
// If the program runs as Java applet it isn't "interactive" in the sense of the GNU
// General Public License. So paragraph 2c doesn't apply.
//
//**************************************************************************************
package mediaframe.mpeg1.controlpanel;

import mediaframe.mpeg1.MPEG1;
import java.awt.Canvas;
import java.awt.*;
import java.awt.event.*;
import java.awt.color.*;
import java.awt.image.*;

/**
 * The <code>ControlButton</code> class represents a button of the <code>ControlPanel</code>.
 * This class responds for downloading button's images and for painting them on the screen.
 *
 * @author  Konstantin Belous
 * @version 1.0 26.07.2002
 */
public final class ControlButton extends Component {

	/** The path to the images. */
	public static String IMAGE_PATH = "control_set/";

	/** The reference to the applet. */
	private MPEG1 player;

	/** The image of the enabled Button. */
	private Image enabledImage;
	/** The image of the disabled button. */
	private Image disabledImage;
	/** The image of the hidden button. */
	private Image hiddenImage;

	/** The x coord of the component. */
	private int x;
	/** The y coord of the component. */
	private int y;

	/**
	 * The constructor of the button. Constructor responds for retrieval of the
	 * button's images and for variables initialization.
	 * 
	 * @param player the reference to the applet.
	 * @param ImagesFileNames an array of the enable/disable 2 image file names.
	 */
	public ControlButton(MPEG1 player, String[] ImagesFileNames) {

		super();

		this.player = player;

		enabledImage =
			player.getImage(player.getDocumentBase(), IMAGE_PATH + ImagesFileNames[0]);
		disabledImage =
			player.getImage(player.getDocumentBase(), IMAGE_PATH + ImagesFileNames[1]);

		try {
			MediaTracker mt = new MediaTracker(this);
			mt.addImage(enabledImage, 0);
			mt.waitForID(0);
		} catch (Exception e) {
			System.out.println(e);
		}

		try {
			MediaTracker mt = new MediaTracker(this);
			mt.addImage(disabledImage, 0);
			mt.waitForID(0);
		} catch (Exception e) {
			System.out.println(e);
		}

		setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
	}

	/** 
	* Loads the hidden image.
	* @param hidden the name of the hidden image
	*/
	public final void loadHidden(String hidden) {

		hiddenImage = player.getImage(player.getDocumentBase(), IMAGE_PATH + hidden);

		try {
			MediaTracker mt = new MediaTracker(this);
			mt.addImage(hiddenImage, 0);
			mt.waitForID(0);

		} catch (Exception e) {
			System.out.println(e);
		}

	}

	/**
	* Rotates enabled and hidden images.
	*/
	public final void rotateHidden() {
		Image tmp = enabledImage;
		enabledImage = hiddenImage;
		hiddenImage = tmp;
	}

	/**
	 * Paints the button.
	 * @param g the graphics context.
	 */
	public final void paint(Graphics g) {

		if (isEnabled() == false) {
			g.drawImage(disabledImage, x, y, this);
			return;
		}

		g.drawImage(enabledImage, x, y, this);
	}

	/**
	 * Sets the x and y coords of the button. 
	 * @param x the x coord to set.
	 * @param y the y coord to set.
	 */
	public final void setBounds(int x, int y) {
		this.x = x;
		this.y = y;
	}

	/**
	 * Sets the speaker button to disabled state.
	 */
	public final void setVolumeDisabled() {
		Image tmp = enabledImage;
		enabledImage = disabledImage;
		disabledImage = tmp;
		repaint();
	}

	/**
	 * Sets the speaker button to enabled state.
	 */
	public final void setVolumeEnabled() {
		Image tmp = enabledImage;
		enabledImage = disabledImage;
		disabledImage = tmp;
		repaint();
	}

}